﻿CREATE VIEW dbo.vwDisgatherDocs
AS
SELECT     D.Id, D.DocDate, D.DocNumber, SP.GoodsId, D.DepartmentUserId, SP.BarCode AS SrcPartyBarCode, SP.RetailPrice AS SrcPrice, D.Note, D.MD, D.MW, 
                      P1.BarCode AS P1BarCode, P1.RetailPrice AS P1RetailPrice, L1.Qntty AS P1Qntty, P2.BarCode AS P2BarCode, P2.RetailPrice AS P2RetailPrice, 
                      L2.Qntty AS P2Qntty
FROM         CD.Parties AS P2 RIGHT OUTER JOIN
                      CA.GoodsDocLines AS L2 ON P2.DocId = L2.DocId AND P2.Id = L2.PartyId RIGHT OUTER JOIN
                      CA.Documents AS D INNER JOIN
                      CA.GoodsDocLines AS SL ON D.Id = SL.DocId AND SL.Qntty = - 1 INNER JOIN
                      CD.Parties AS SP ON SL.PartyId = SP.Id INNER JOIN
                      CD.Parties AS P1 ON D.Id = P1.DocId AND D.Id = P1.DocId AND D.ContragentDocNumber = P1.BarCode INNER JOIN
                      CA.GoodsDocLines AS L1 ON P1.Id = L1.PartyId AND P1.DocId = L1.DocId ON P2.BarCode <> D.ContragentDocNumber AND P2.DocId = D.Id AND 
                      P2.DocId = D.Id
WHERE     (D.DocTypeId = 12)

GO
EXECUTE sp_addextendedproperty @name = N'MS_DiagramPane1', @value = N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[35] 4[45] 2[10] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = -96
         Left = -288
      End
      Begin Tables = 
         Begin Table = "D"
            Begin Extent = 
               Top = 127
               Left = 410
               Bottom = 403
               Right = 601
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "SL"
            Begin Extent = 
               Top = 266
               Left = 194
               Bottom = 502
               Right = 333
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "SP"
            Begin Extent = 
               Top = 4
               Left = 192
               Bottom = 246
               Right = 327
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "P1"
            Begin Extent = 
               Top = 0
               Left = 727
               Bottom = 241
               Right = 879
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "L1"
            Begin Extent = 
               Top = 0
               Left = 960
               Bottom = 224
               Right = 1112
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "P2"
            Begin Extent = 
               Top = 275
               Left = 727
               Bottom = 513
               Right = 887
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "L2"
            Begin Extent = 
               Top = 274
               Left = 960
               Bottom = 426
               Right = 1112
            End
            DisplayFlags = 280
            TopColumn = 0
', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'VIEW', @level1name = N'vwDisgatherDocs';


GO
EXECUTE sp_addextendedproperty @name = N'MS_DiagramPane2', @value = N'         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 17
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 2475
         Alias = 1875
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'VIEW', @level1name = N'vwDisgatherDocs';


GO
EXECUTE sp_addextendedproperty @name = N'MS_DiagramPaneCount', @value = 2, @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'VIEW', @level1name = N'vwDisgatherDocs';

